<?php

namespace App\Http\Controllers;

use App\Entities\ClassEntity;
use App\Entities\ClassMember;
use App\Entities\Member;
use Illuminate\Http\Request;
use App\Repositories\MemberRepository;
use App\Repositories\ClassMemberRepository;

class MembersController extends Controller
{
    private $member;
    private $classMember;


    public function __construct(
        MemberRepository $member,
        ClassMemberRepository $classMember
    )
    {
        $this->member = $member;
        $this->classMember = $classMember;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $condition = [];
        if($request->get('name')){
            $condition['name'] = ['like','%'.$request->get('name').'%'];
        }
//        $condition['status'] = 1;

        $list = $this->member->paginateWithCondition(10, ['classMember'], $condition);

//        dd($list);
        foreach ( $list->items() as $k => $v ) {
            if($v->classMember){
                foreach ($v->classMember as $kk => $vv) {
//                    var_dump($vv);
                    $vv->classEntity  = ClassEntity::with(['course','term'])->find($vv->class_id);
                }
            }
        }
//        die;
        return response()->json([
            'errors' => false,
            'count' => $list->count(),
            'data' => $list
        ]);
    }


    public function getMemberByIds(Request $request) {
//        dump($request->all());
        $members = Member::find($request->all());

        return response()->json([
            'errors' => false,
            'data' =>$members
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $member = $this->member->create($request->all());
        return response()->json([
            'errors' => false,
            'message' => '新建成功',
            'data' => $member
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Entities\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $member = $this->classEntity->find($id);
        return response()->json([
            'errors' => false,
            'data' => $member
        ]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Entities\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $member = $this->member->find($id);
        $newMember = $this->member->update($member, $request->all());
        return response()->json([
            'errors'=> false,
            'data' => $newMember
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Entities\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $member = $this->member->find($id);
//        var_dump($id);die;
        $ret = $this->member->destroy($member);
        if($ret) {
            //TODO 这里需要做级联软删除
//            ClassMember::where('member_id', $id)->update(['status','0']);
            ClassMember::where('member_id', $id)->delete();

        }

        return response()->json([
            'errors' =>  false,
            'data' => $ret
        ]);
    }
}
